/*******************************************************************************
*  Filename:        per_test_cc254x.c
*  Revised:         $Date: 2013-04-29 13:31:25 +0200 (ma, 29 apr 2013) $
*  Revision:        $Revision: 9926 $
*
*  Description:     Packet Error Rate (PER) test for the CC2541EM, CC2543EM, 
*                   CC2544Dongle and the CC2545EM.
*
*  note             This code is made for range evaluation as well as for use as 
*                   example code showing how to use the radio. The radio can be 
*                   utilized in more ways than what is used in this example code.
*
*  warning          This program is intended for use between one single master 
*                   device and one single slave. Make sure not to have multiple 
*                   Master devices powered up while performing test!
*
*  Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
*
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*    Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*
*    Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
*
*    Neither the name of Texas Instruments Incorporated nor the names of
*    its contributors may be used to endorse or promote products derived
*    from this software without specific prior written permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
*  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
*  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
*  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
*  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
*  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*******************************************************************************/


/*******************************************************************************
* INCLUDES
*/
// Include device specific files
#include "main.h"




/*******************************************************************************
* GLOBAL VARIABLES
*/


// Global flags.
extern volatile uint8 rfirqf1;
extern uint8 RadioTimeoutFlag;

uint16_t Crc;
uint8_t TxBuff[9] = {1, 2, 3, 4, 5};
extern UINT32_VAL TxId;


/*******************************************************************************
* @fn          main
*
* @brief       Main program
*
* @param       void
*
* @return      int (does not return)
*/

int main(void) {
    // Main auto variables.

    /* Initialize Clock Source (32 Mhz Xtal),
    *  global interrupt (EA=1),  I/O ports and pheripherals(LCD). */

    P0DIR |= 1 << 0;  
    //P0_3 = 1;
    
    halBoardInit();

    ActionTickInit();

    EnableGlobalInterrupt();

    //RFConnectInit();

    while(1)
    {
        Crc = CalDataCRC(TxId.Val, TxBuff, 5);
        //memcpy(TxBuff + 5, &Crc, 2);
        TxBuff[5] = (uint8)(Crc >> 8);
        TxBuff[6] = (uint8)(Crc);
        halRfPrepareTxPacket(7, TxBuff);
        // Start transmitter
        halRfStartTx();          
    
        // Wait for TASKDONE and halt CPU (PM0) until task is completed.
        while (!(rfirqf1 & RFIRQF1_TASKDONE)) {
        }


        // Reset TXFIFO to clear any remaining data in buffer.
        halRfCommand(CMD_TXFIFO_RESET);
        // Clear the global RFIRQF1 shadow variable (RF Interrupt flags).
        // Set to a undefined value in enum type end cause.
        PRF.ENDCAUSE = TASK_UNDEF;

        halMcuWaitMs(3);
    }
}
